package org.NooLab.glue.components;

import org.NooLab.glue.components.pkg.TaskPackage;
import org.NooLab.glue.interfaces.ReceptorDigestIntf;
import org.NooLab.glue.msgframe.XMessage;
import org.NooLab.glue.msgframe.XRegistration;
import org.NooLab.glue.subscription.FilterPackageIntf;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.net.GUID;





public class ParticipantsSysMsgInterpreter {

	MessageBoardFactory mbf ;
	XRegistration xRegistration;
	
	ReceptorDigestIntf resultsReceiver ;
	
	XMessage xmsg;
	
	String nameOfInstance;
	int protocolID;
	
	PrintLog out ;
	
	
	
	public ParticipantsSysMsgInterpreter( MessageBoardFactory mbf, 
			  							  XRegistration xreg, 
			  							  ReceptorDigestIntf resultsreceiver,
			  							  String nameofinstance,
			  							  PrintLog outprn){

		this.mbf = mbf ;
		
		nameOfInstance = nameofinstance;
		
	 
		xRegistration = xreg;
		 
		resultsReceiver = resultsreceiver;
		 
		
		protocolID = mbf.getComBoxFactory().getProtocolID();
		
		out = outprn;
		
	}
	
	public int interpretingSysMessage( String rawXmlStr ){
		
		int result = -1;
		
		boolean hb ;
		String roleOfSender ;
		
		String[] rawIndicators, rawOptionalIndicators ;
	  
		
		
		xmsg = new XMessage(out) ;
		
		result = 0;
		
		// <messageboard name="spela" role="BOARD">
		roleOfSender = xmsg.getSpecifiedInfo(rawXmlStr,"//messageboard","role").toLowerCase() ;  

		
		rawIndicators = new String[]{"<register>","<check","\"ping\""} ;
		if (handlingSysMsg_ping( xmsg, rawXmlStr , rawIndicators) ){ return result; }

		rawIndicators = new String[]{"<shutdown","<system" } ;
		if (handlingSysMsg_shutdowns( xmsg, rawXmlStr , rawIndicators) ){ return result; }
		
		rawIndicators = new String[]{"<disconnected","<system" } ;
		if (handlingSysMsg_disconnectedBoard( xmsg, rawXmlStr , rawIndicators) ){ return result; }
		
		result = -3;
		
		return result ;
	}
	
	
	private boolean handlingSysMsg_shutdowns( XMessage xmsg, String rawXmlStr , String[]rawIndicators ){
		boolean result = false;
		long iid;
		
		result = xmsg.rawIndicatorsCheck( rawXmlStr, rawIndicators );
		
		if (result){
			
			iid = xmsg.getSpecifiedValue(rawXmlStr, "//system/shutdown", "id") ;
			if (iid<0){
				iid = xmsg.getSpecifiedValue(rawXmlStr, "//system/restart", "id") ;
			}
			
			if ( (rawXmlStr.contains("restart"))  ){
				// re-register in 30 seconds with current ID and GUIDed name
				out.printErr(1, "Instance received a restart message from the MessageBoard!\n"+
								"Instance will attempt to re-register in a few seconds...");
				resultsReceiver.performSystemTask( "restart") ;
			}else{
				out.printErr(1, "Instance received a shutdown message from the MessageBoard!\n\r"+
								"This instance will shut down, too.");
				resultsReceiver.performSystemTask( "shutdown") ;
			}
			
		}
		
		return result;
	}

	private boolean handlingSysMsg_disconnectedBoard( XMessage xmsg, String rawXmlStr , String[]rawIndicators ){
		boolean result = false;
		long iid;
		
		result = xmsg.rawIndicatorsCheck( rawXmlStr, rawIndicators );
		
		if (result){
		
			iid = xmsg.getSpecifiedValue(rawXmlStr, "//system/disconnected", "id") ;
			
			if (xRegistration.getInstanceID()==iid){
				resultsReceiver.performSystemTask( "disconnected") ;
			}
		}
		
		return result;
	}

	
	private boolean handlingSysMsg_ping( XMessage xmsg, String rawXmlStr , String[]rawIndicators ){
		boolean result = false;
		
		result = xmsg.rawIndicatorsCheck( rawXmlStr, rawIndicators );
		
		if (result){
			resultsReceiver.performSystemTask( "ping::" + rawXmlStr) ;
		}
		
		return result;
	}

	
 
}
